package com.tianpengtech.admin.task;
import org.apache.shiro.authz.annotation.RequiresAuthentication;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.admin.AbstractAdminController;
import com.tianpengtech.admin.IAdminOpt;
import com.tianpengtech.admin.InitInterceptor;
import com.tianpengtech.common.GlobalConfigConstant;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.feedback.FeedBack;
import com.tianpengtech.common.model.opertion.Opertion;
import com.tianpengtech.common.model.task.Task;
import com.tianpengtech.common.util.JsonUtil;
/**
* 路由配置地址：add("/admin/task", TaskAdminController.class, "/admin/task");
* 数据库映射地址：arp.addMapping(Task.dao.tableName(),Task.class);
*/
@Before(InitInterceptor.class)
@RequiresAuthentication
public class TaskAdminController extends AbstractAdminController{
	private IAdminOpt opt = new TaskAdminServiceImpl();
	@Override
	protected void config() {
		this.clazz=Task.class;
		this.service=new TaskAdminServiceImpl();
	}

	@Override
	public void beforeAdd() {

	}

	@Override
	public void beforeAdd(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeDelete(Model< ? > model) {

	}
	
	
	/**
	 * 审核通过
	 */
	public void  pass(){
		int id = getParaToInt(Task.ID,0);
		try {
			opt.changeStatus(getModel(Task.class), id, Task.STATUS_SUCCESS, Kv.create());
			renderJson(JsonUtil.printSuccess("操作成功"));
		} catch (ValidateException e) {
			renderJson(JsonUtil.printError(e.getMessage()));
		}
	}
	
	/**
	 * 拒绝理由填写页面
	 */
	public void  toRefuse(){
		int id = getParaToInt(Task.ID,0);
		setAttr("task",this.service.queryById(getModel(Task.class), id));
		renderTemplate("task-reason.html");
	}
	
	/**
	 * 审核不通过
	 */
	public void  unpass(){
		Task task = getModel(Task.class);
		String content = getPara(Opertion.CONTENT);
		Kv kv = Kv.by(Opertion.CONTENT, content);
		try {
			opt.changeStatus(task, task.getLong(Task.ID), Task.STATUS_FAILURE, kv);
			renderJson(JsonUtil.printSuccess("操作成功"));
		} catch (ValidateException e) {
			renderJson(JsonUtil.printError(e.getMessage()));
		}
	}

	@Override
	public void parsePageRecord(Object results) {
		Task task = (Task)results;
		task.set(Task.STATUS, task.status());
	}

	
	public void showDetail(){
		int id = getParaToInt(Task.ID, GlobalConfigConstant.DEFAULT_VALUE_ZERO);
		setAttr("task", this.service.queryById(getModel(Task.class), id));
		renderTemplate("task-detail.html");
	}
}

	